<template>
  <div class="main-content">
    <el-card style="width: 50%; margin: 30px auto" v-loading="loading">
      <div style="text-align: right; margin-bottom: 20px">
        <el-button type="primary" @click="updatePassword">修改密码</el-button>
      </div>
      <el-form :model="user" label-width="80px" style="padding-right: 20px">
        <div style="margin: 15px; text-align: center">
          <el-upload disabled
              class="avatar-uploader"
              :action="$baseUrl + '/files/upload'"
              :show-file-list="false"
          >
            <img v-if="avatarURL" :src="avatarURL" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </div>
        <el-form-item label="账户名" prop="userAccount">
          <el-input v-model="user.userAccount" placeholder="账户名"></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="userName">
          <el-input v-model="user.userName" placeholder="姓名"></el-input>
        </el-form-item>
        <div style="text-align: center; margin-bottom: 20px">
          <el-button type="primary" @click="update">保 存</el-button>
        </div>
      </el-form>
    </el-card>
    <el-dialog title="修改密码" :visible.sync="dialogVisible" width="30%" :close-on-click-modal="false" destroy-on-close>
      <el-form :model="user" label-width="80px" style="padding-right: 20px" :rules="rules" ref="formRef">
        <el-form-item label="原始密码" prop="password">
          <el-input show-password v-model="user.originPassword" placeholder="原始密码"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="newPassword">
          <el-input show-password v-model="user.newPassword" placeholder="新密码"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input show-password v-model="user.confirmPassword" placeholder="确认密码"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="fromVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    const validatePassword = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请确认密码'))
      } else if (value !== this.user.newPassword) {
        callback(new Error('确认密码错误'))
      } else {
        callback()
      }
    }
    return {
      user: {},
      dialogVisible: false,
      avatarURL: 'https://img2.baidu.com/it/u=758681469,2261305630&fm=253&fmt=auto&app=120&f=JPEG?w=608&h=433',
      rules: {
        originPassword: [
          { required: true, message: '请输入原始密码', trigger: 'blur' },
        ],
        newPassword: [
          { required: true, message: '请输入新密码', trigger: 'blur' },
        ],
        confirmPassword: [
          { validator: validatePassword, required: true, trigger: 'blur' },
        ],
      },
      loading: false
    }
  },
  created() {
    this.getUserInfo()
  },
  methods: {
    async getUserInfo() {
      this.loading = true
      await this.$request.post('/user/getUserInfo').then((res) => {
        if (res.code === 200) {
          this.user = res.data
        }
      }).finally(() => {
        this.loading = false
      })
    },
    update() {
      // 保存当前的用户信息到数据库
      this.$request.post('/user/updateUserInfo', {
        id: this.user.id,
        userAccount: this.user.userAccount,
        userName: this.user.userName,
      }).then(res => {
        if (res.code === 200) {
          // 成功更新
          this.$message.success('保存成功')
          // 更新浏览器缓存里的用户信息
          // localStorage.setItem('xm-user', JSON.stringify(this.user))
          // 触发父级的数据更新
          this.$emit('update:user')
        } else {
          this.$message.error(res.message)
        }
      })
    },
    // 修改密码
    updatePassword() {
      this.dialogVisible = true
    },
    save() {
      this.$refs.formRef.validate((valid) => {
        if (valid) {
          this.$request.post('/user/updatePassword', {
            id: this.user.id,
            originPassword: this.user.originPassword,
            newPassword: this.user.newPassword,
            confirmPassword: this.user.confirmPassword
          }).then(res => {
            if (res.code === 200) {
              // 成功更新
              this.$message.success('修改密码成功')
              this.$request.post('/user/logout')
              this.$router.push('/login')
            } else {
              this.$message.error(res.message)
            }
          })
        }
      })
    }
  }
}
</script>

<style scoped>
/deep/.el-form-item__label {
  font-weight: bold;
}
/deep/.el-upload {
  border-radius: 50%;
}
/deep/.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  border-radius: 50%;
}
/deep/.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 120px;
  height: 120px;
  line-height: 120px;
  text-align: center;
  border-radius: 50%;
}
.avatar {
  width: 120px;
  height: 120px;
  display: block;
  border-radius: 50%;
}
</style>